package org.deltaset.faces.upload;

import java.util.Map;
import org.deltaset.faces.FacesUtils;

/**
 *
 * @author dmakariev
 */
class FileSessionCache {

    String fileSessionKey(CurrentFile currentFile) {
        if (null == currentFile) {
            return "null-file";
        }
        final String key = createKey(currentFile);
        final Map<String, Object> session = FacesUtils.getSessionMap();
        if (!session.containsKey(key)) {
            session.put(key, currentFile);
        }
        return key;
    }

    CurrentFile resolveFile(String key) {
        final Map<String, Object> session = FacesUtils.getSessionMap();
        return (CurrentFile) session.get(key);
    }
    
    private String createKey(final CurrentFile currentFile){
         return currentFile.getName() +"-" + currentFile.getContentType() + "-" + currentFile.getFileSize();
    }
}
